package com.bq.gametrade.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.TransferBilAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.TransferBill;
import com.bq.gametrade.bean.TransferBillDetails;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.ui.dialog.CustomPickDateDialog;
import com.bq.gametrade.utils.DateUtils;
import com.bq.gametrade.utils.NetworkUtils;
import com.bq.gametrade.utils.ToastUtils;
import com.bq.gametrade.view.pullableview.PullToRefreshLayout;
import com.bq.gametrade.view.pullableview.PullableListView;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TransferBillActivity extends BaseActivity implements View.OnClickListener {

    private ImageView back;
    private TextView tvTitle;
    private TextView tvStartTime, tvEndTime;
    private Button toQuery;

    private PullToRefreshLayout refreshLayout;
    private PullableListView listView;
    private TransferBilAdapter adapter;
    private ArrayList<TransferBillDetails> mList = new ArrayList<>();
    private LinearLayout layPrompt;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transferbill);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        initView();
        initListView();
        getTransferBill(DateUtils.getLastWeakDate(),DateUtils.getDate());
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("转账账单");

        refreshLayout = serchViewById(R.id.layout_refresh);
        listView = serchViewById(R.id.lv_transferBil);
        adapter = new TransferBilAdapter(TransferBillActivity.this, mList, application.getUserInfo());
        listView.setAdapter(adapter);
        layPrompt = serchViewById(R.id.lay_prompt);

        tvStartTime = serchViewById(R.id.tv_startTime);
        tvStartTime.setText(DateUtils.getLastWeakDate());
        tvStartTime.setOnClickListener(this);
        tvEndTime = serchViewById(R.id.tv_endTime);
        tvEndTime.setText(DateUtils.getDate());
        tvEndTime.setOnClickListener(this);
        toQuery = serchViewById(R.id.btn_toQuery);
        toQuery.setOnClickListener(this);
    }


    private void initListView() {


        refreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
//                listViewInfo(1,pullToRefreshLayout,0);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                toastShow("刷新完成");

            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 加载操作
//                listViewInfo(1,pullToRefreshLayout,1);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                toastShow("加载完成");

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image_back:
                finish();
                break;
            case R.id.tv_startTime:
                CustomPickDateDialog dateDialog = new CustomPickDateDialog(TransferBillActivity.this,
                        DateUtils.formatDateChinese(new Date()));
                dateDialog.dateTimePicKDialog(tvStartTime);
                break;
            case R.id.tv_endTime:
                CustomPickDateDialog myDateDialog = new CustomPickDateDialog(TransferBillActivity.this,
                        DateUtils.formatDateChinese(new Date()));
                myDateDialog.dateTimePicKDialog(tvEndTime);
                break;
            case R.id.btn_toQuery:
                if (!application.isLogin()) {
                    ToastUtils.showToast(TransferBillActivity.this, "您还没有登陆");
                    return;
                }
                if (tvStartTime.getText().toString().equals("起始时间")) {
                    ToastUtils.showToast(TransferBillActivity.this, "起始时间未输入");
                    return;
                }
                if (tvEndTime.getText().toString().equals("结束时间")) {
                    ToastUtils.showToast(TransferBillActivity.this, "结束时间未输入");
                    return;
                }
                if (DateUtils.compareDate(tvStartTime.getText().toString(), tvEndTime.getText().toString()) == -1) {
                    ToastUtils.showToast(TransferBillActivity.this, "结束时间不能小于起始时间");
                    return;
                }
                if (NetworkUtils.isNetworkAvailable(TransferBillActivity.this)) {
                    getTransferBill(tvStartTime.getText().toString(), tvEndTime.getText().toString());

                } else {
                    ToastUtils.showToast(TransferBillActivity.this, "没有网络了····");
                }

                break;
        }
    }

    private void getTransferBill(String startTime,String endTime) {
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("stime", startTime);
        params.put("etime", endTime);
        showSpotsDialog();
        VRHttp.getTransferBill(TransferBillActivity.this, HttpLink.TRANSFER_BILL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {
                    TransferBill transferBill = (TransferBill) response;
                    if (transferBill.status.equals("0")) {
                        mList = transferBill.user_transfer_list;
                        if(mList.size()>0){
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }

                    } else if (transferBill.status.equals("99")) {
                        toastShow("登录超时");
                        application.setUserInfo(null);
                        startActivity(new Intent(TransferBillActivity.this, LoginActivity.class));
                        finish();
                    } else if (transferBill.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(TransferBillActivity.this, LoginActivity.class));
                        finish();
                    } else {
                        toastShow("获取转账账单失败,请重试");
                    }
                } else {
                    toastShow("获取转账账单失败,请重试");
                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取数据出错");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
